<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class ProgramResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        if ($this->program_file != null) {
            return [
                'id' => $this->id,
                'name' => $this->name,
                'type' => optional($this->type)->name,
                'versionnumber' => $this->versionnumber,
                'program_file' => env('FILE_PATH') . $this->program_file,
                'download' => $this->download,
                'fize_size' => $this->getSize($this->program_file).'M',
                'description' => $this->description,
                'logo' => $this->imgs,
            ];
        }
        return [
            'id' => $this->id,
            'name' => $this->name,
            'type' => optional($this->type)->name,
            'versionnumber' => $this->versionnumber,
            'program_file' => $this->program_file,
            'download' => $this->download,
            'fize_size' => $this->getSize($this->program_file).'M',
            'description' => $this->description,
            'logo' => $this->imgs,
        ];
    }
    public function getSize($file){
            $url = env('FILE_PATH') . $file;
            $res = get_headers($url,true);
            $size = $res['Content-Length']/1024/1024;
            return sprintf('%.2f',$size);
    }
}
